local Obj = require("core.Obj")
local Msg = require("core.Msg")
local Timer = require("core.Timer")
local ExcelCollectConf = require("excel.Collect")
local EffectLogic = require("scene.EffectLogic")
local RoleFFI = require("role.RoleFFI")
local Map = require("core.Map")

list_reborn = list_reborn or {} -- 等待重生的采集物列表 id -> rebornTime
list_destroy = list_destroy or {} -- 一段时间后自己销毁的采集物列表 id -> destroyTime

function create(collect_id, ownerUuid, bodyID, destroyTime, dropMonsterID)
	local cf = ExcelCollectConf.Collect[collect_id]
	if not cf then
		assert(nil, collect_id)
	end
	local collect = {}
	Obj.create(collect, Obj.TYPE_COLLECT)
	collect.collect_id = collect_id
	collect.bodyID = bodyID  or cf.bodyID
	collect.body_scale = bodyID and 1 or cf.bodyScale

	collect.ownerUuid = ownerUuid
	collect.destroyTime = destroyTime or cf.destroyTime
	collect.dropMonsterID = dropMonsterID
	if #cf.dir == 0 then
		collect.dir = 0
	else
		local rand = math.random(1, #ExcelCollectConf.Collect[collect_id].dir)
		collect.dir = cf.dir[rand]
	end
	
	if collect.destroyTime > 0 then
		list_destroy[collect.id] = {uid = collect.uid, destroyTime = Timer.now + collect.destroyTime * 1000 - 500}
	end
	
	return collect
end


function getName(collect)
	if collect.name then
		return collect.name
	else
		return ExcelCollectConf.Collect[collect.collect_id].name
	end
end

function getObjAdd(collect)
	local config = ExcelCollectConf.Collect[collect.collect_id] 
	local msgRet = Msg.gc.GC_ADD_COLLECT
	msgRet.obj_id = collect.id
	msgRet.name = getName(collect)
	if collect.body_id then
		msgRet.body_id = collect.body_id
		msgRet.body_scale = 1
		msgRet.effect_id = 0
		msgRet.bone = collect.body_bone or 0
	else
		if collect.collected == nil then
			msgRet.body_id = collect.bodyID
			msgRet.body_scale = collect.body_scale
			msgRet.effect_id = config.body_effect_id
			msgRet.bone = config.isBoneID
		else
			msgRet.body_id = config.bodyID2
			msgRet.effect_id = 0
			msgRet.bone = config.isBoneID2
			msgRet.body_scale = config.bodyScale2
		end
	end
	msgRet.body_effect = config.bodyID3
	msgRet.effect_config = EffectLogic.getConfig(config.body_effect_id)
	msgRet.dir = collect.dir
	msgRet.x = collect.x
	msgRet.y = collect.y
	msgRet.collect_id = collect.collect_id
	msgRet.bindTask1 = config.bindTask1
	msgRet.bindTask2 = config.bindTask2
	msgRet.bornCmd = collect.show_after_born or 0
	msgRet.interactTask = config.interactTask
	msgRet.interactAim = config.interactAim
	msgRet.disappearClient = config.disappearClient
	msgRet.dailyTask = config.dailyTask
	msgRet.needQuery = config.onlyOnce
	msgRet.isShowShadow = config.isShowShadow
	if collect.jindu then
		msgRet.xue_ren_jin_du_cur = collect.jindu
	else
		msgRet.xue_ren_jin_du_cur = -1
	end
	return msgRet
end

function destroy(collect)
	if collect.can_not_destroy then
		return
	end
	list_reborn[collect.id] = nil
	list_destroy[collect.id] = nil
	Obj.destroy(collect)
end

function enterScene(collect, scene_id, x, y)
	local cf = getConfig(collect)
	local body_id = cf.born_effect_id
	if 0 < body_id then
		local mm = Msg.gc.GC_SHOW_DISPLAY
		mm.body_id = body_id
		mm.x = x
		mm.y = y
		mm.effectConfig = EffectLogic.getConfig(body_id)
		Obj.sendArea(mm, scene_id, x, y)
	end
	if cf.show_after_born > 0 then
		collect.show_after_born = cf.show_after_born
	end
	collect.born_x = x
	collect.born_y = y
	collect.born_scene_id = scene_id
	Obj.enterScene(collect, scene_id, x, y)
	collect.show_after_born = nil
end

function getConfig(collect)
	return ExcelCollectConf.Collect[collect.collect_id] 
end

function isOwner(collect, human)
	if collect.ownerUuid == nil then return true end
	if collect.ownerUuid == human._id then return true end
  for i = 0, human.db.teamUuidLen - 1 do
     if RoleFFI.ffi_string(human.db.teamUuid[i].uuid) == collect.ownerUuid then
         return true
     end
  end
end

function checkReborn()
	for objID, rebornInfo in pairs(list_reborn) do
		if rebornInfo.reborn <= Timer.now then
			local collect = Obj.getObj(objID, rebornInfo.uid)
			if collect and Obj.scenes[collect.born_scene_id] then
				if collect.scene_id then
					collect.collected = nil
					collect.ownerUuid = nil
					collect.die = 1				
					Obj.leaveScene(collect)
				else
					collect.die = 0
                    if rebornInfo.area_id then
                        local map_id = Map.scene_id_to_map_id[collect.born_scene_id]
                        local list_rect = Map.getRects(map_id, rebornInfo.area_id)
                        local rect = list_rect[math.random(1, #list_rect)]
                        enterScene(collect, collect.born_scene_id, math.random(rect[1], rect[3]), math.random(rect[2], rect[4]))
                    else
                        enterScene(collect, collect.born_scene_id, collect.x, collect.y)
                    end
				end
			end
			list_reborn[objID] = nil
		end
	end
	
	for objID, info in pairs(list_destroy) do
		if info.destroyTime <= Timer.now then
			local collect = Obj.getObj(objID, info.uid)
			if collect then
				destroy(collect)
			end
		end
	end
end
